programming4us
           
 
 
Windows Phone

Developing Applications for Windows Phone 7 : XAML Styling (part 2)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/27/2010 11:49:23 AM

Control Templates

Every control in Silverlight has XAML that defines how a control is drawn. For example, the humble button uses this XAML to draw itself:

<Grid Background="Transparent">
...
<Border x:Name="ButtonBackground"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
CornerRadius="0"
Margin="{StaticResource PhoneTouchTargetOverhang}">
<ContentControl x:Name="ContentContainer"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="{TemplateBinding
HorizontalContentAlignment}"
Padding="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding
VerticalContentAlignment}" />
</Border>
</Grid>

Even though you think of controls as atomic elements, there is XAML inside the control to define the look and feel of the control. Control templates are used to re-define this XAML for any control.

Control templates are part of the style that is applied to a control. The ControlTemplate is the value of the Template property of any control. For example:

<Style x:Key="ButtonStyle1"
TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
...
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

The contents of the ControlTemplate contains the XAML that the particular control should use (i.e. Button in this case). As you define the XAML that makes up the look of a particular control, you can use a markup extension called TemplateBinding to pull the value of a property into your XAML. For example:

<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<Border x:Name="ButtonBackground"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
...
</Border>
</Grid>
</ControlTemplate>

By using template bindings, the natural value of the property will be used as the value inside the control template. This value could come from the default value in the control, a style setter or specifically set on an instance of the control. Template bindings allow you to use whatever property value is supposed to be show in the control.

For some controls, the XAML must have certain elements to make sure the control still works. For example the WebBrowser control requires that there be a part of the XAML that is a Border element named PresentationContainer. That way the control knows where to show the web content. This contract between you and the control author is called a TemplatePart (many controls do not have any template parts.). The template parts that are required are documented as attributes on the controls themselves (as seen in Figure 1).

Figure 1. TemplatePart Attribute (TemplatePart.bmp)


The structure of the XAML in a control template represents the look of the control, but in addition you can specify the feel of an application as well. The feel of an application is the way that the control can interact with the users. For example, the Button class changes its appearance when pressed to give that feedback to the user that they’ve correctly pressed the button. This is the way that the feel of an application works.

The feel of an application can be created using a structure called the Visual State Manager. The visual state manager is a way of defining animations that represent a state that the control can be in. The visual state manager is used by the control to go into specific states as the user interacts with it. As the application designer you can define these states to change the way that the control interacts with the user. These states are broken up in to groups so a single control can be in more than one state. For example, two of the visual state manager groups that the TextBox has are CommonStates (which represent states like Disabled and ReadOnly) and FocusStates (which represent whether the control has focus or not). You could imagine that a control could be in both an unfocused state and be disabled.

To define the groups and states for the visual state manager, the XAML can contain a VisualStateManager.VisualStateGroups property (as an attached property):

<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetProperty="Foreground"
Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource
PhoneBackgroundBrush}" />
</ObjectAnimationUsingKeyFrames>
...
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetProperty="Foreground"
Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource
PhoneDisabledBrush}" />
</ObjectAnimationUsingKeyFrames>
...
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
...
</Grid>
</ControlTemplate>


As this example shows, the VisualStateManager.VisualStateGroups attached property contains one (or more) VisualStateGroup objects. Inside the group is a list of VisualState objects that represent a Storyboard that shows how to go to a specific state. The empty VisualState objects mean that that state should look exactly like the natural state of the object.

The visual states and groups that a control supports are also specified as attributes on the control classes as seen in Figure 2.

Figure 2. TemplateVisualState Attribute (TemplateVisualState.bmp)


When creating your own control templates you will need to be aware of the template parts and template visual states as that is the contract between you and the control author. You must implement these states and parts if you expect the controls to continue to operate correctly.

Other -----------------
- Developing Applications for Windows Phone 7 : Data Binding (part 2)
- Developing Applications for Windows Phone 7 : Data Binding (part 1)
- Developing Applications for Windows Phone 7 : Transformations and Animations
- Developing Applications for Windows Phone 7 : Controls
- Developing Applications for Windows Phone 7 : Visual Grammar
- Developing Applications for Windows Phone 7 : Visual Containers
- Developing Applications for Windows Phone 7 : What is XAML?
- Windows Phone 7 : Connecting a Bluetooth Headset
- Windows Phone 7 : Turning On Airplane Mode
- Windows Phone 7 : Updating Your Phone Software
- Windows Phone 7 : Finding a Lost Phone
- Windows Phone 7 : Locking Your Phone
- Windows Phone 7 : Importing Contacts from a SIM Card
- Windows Phone 7 : Silencing Your Phone
- Windows Phone 7 with Silverlight : Working with the Phone
- Writing Your First Phone Application - Adding Code (part 2)
- Writing Your First Phone Application - Adding Code (part 1)
- Developing Applications for Windows Phone 7 : Designing with Blend
- Developing Applications for Windows Phone 7 : Creating a New Project
- Developing Applications for Windows Phone 7 with Silverlight : Preparing Your Machine
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us